import { toSuccess, useRedis } from "@sker/core"
import { useData } from "@sker/runtime"
import dayjs from "dayjs"
import { z } from "zod"


export default async () => {
    const input = z.object({
        uuid: z.string(),
        data: z.any()
    })
    const body = input.parse(useData())
    await useRedis(8, async r => {
        const key = `red.${body.uuid}`
        const value = await r.get(key)
        if (value) {
            const json = JSON.parse(value);
            const bodyValue = body.data;
            await r.set(key, JSON.stringify({
                ...json,
                ...bodyValue,
            }))
        }
        
        const endDate = dayjs().endOf('day').toDate().getTime()
        const expirationTime = Math.floor((endDate - new Date().getTime()) / 1000)
        await r.expire(key, expirationTime);
    })
    return toSuccess(true)
}
